package com.sunrise.xdoc.entity.user;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.sunrise.xdoc.entity.AbstractCodeEntity;

@Entity
@Table(name = "security_user")
@Inheritance(strategy = InheritanceType.JOINED)
public class User extends AbstractCodeEntity implements Serializable, UserDetails {
	private static final long serialVersionUID = 1L;

	private String username;

	private String password;

	private Set<Role> roles = new HashSet<Role>();

	private boolean accountNonExpired = true;

	private boolean accountNonLocked = true;

	private boolean credentialsNonExpired = true;

	private boolean enabled = true;

	public void setAccountNonExpired(boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	public void setAccountNonLocked(boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	public void setCredentialsNonExpired(boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	@Column(name = "password")
	@Length(max = 32)
	@NotBlank
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "username", unique = true)
	@NotBlank
	@Length(max = 50)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "isAccountNonExpired", nullable = true)
	@Override
	public boolean isAccountNonExpired() {
		return accountNonExpired;
	}

	@Column(name = "isAccountNonLocked", nullable = true)
	@Override
	public boolean isAccountNonLocked() {
		return accountNonLocked;
	}

	@Column(name = "isCredentialsNonExpired", nullable = true)
	@Override
	public boolean isCredentialsNonExpired() {
		return credentialsNonExpired;
	}

	@Column(name = "isEnabled", nullable = true)
	@Override
	public boolean isEnabled() {
		return enabled;
	}

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "au_user_role", joinColumns = { @JoinColumn(name = "id_user") }, inverseJoinColumns = { @JoinColumn(name = "id_role") })
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@Transient
	@Override
	public Set<? extends GrantedAuthority> getAuthorities() {
		return roles;
	}

	@Override
	public String toString() {
		return username;
	}

}
